#include <iostream>
#include <vector>
#include <string>
#include "log.hpp"
#include <boost/filesystem.hpp>
#include "util.hpp"

const std::string input = "data/input";
const std::string output = "data/raw_html/raw.txt";

typedef struct DocInfo
{
    std::string _title;
    std::string _content;
    std::string _url;
}DocInfo_t;

bool EnumFile(const std::string& input, std::vector<std::string> *file_list);
bool ParseHtml(const std::vector<std::string>& file_list, std::vector<DocInfo_t> *results);
bool SaveHtml(const std::string& output, const std::vector<DocInfo_t>& results);

int main()
{
    std::vector<std::string> files_list;
    if(!EnumFile(input, &files_list))
    {
        LOG(FATAL, "EnumFile error!");
        return 1;
    }
    std::vector<DocInfo_t> results;
    if(!ParseHtml(files_list, &results))
    {
        LOG(FATAL, "ParserHtml error!");
        return 2;
    }

    if(!SaveHtml(output, results))
    {
        LOG(FATAL, "SaveHtml error!");
        return 3;
    }
    
    return 0;
}

bool EnumFile(const std::string& input, std::vector<std::string> *files_list)
{
    namespace fs = boost::filesystem;
    fs::path root_path(input);

    if(!fs::exists(root_path)){
        LOG(FATAL, input + "not exists!");
        return false;
    }

    fs::recursive_directory_iterator end;
    for(fs::recursive_directory_iterator iter(root_path); iter != end; iter++)
    {
        if(!fs::is_regular_file(*iter)) continue;
        if(iter->path().extension() != ".html") continue;
        files_list->push_back(iter->path().string());
    }
    return true;
}

bool ParseTitle(const std::string& file, std::string& title)
{
    std::size_t begin = file.find("<title>");
    if(begin == std::string::npos) return false;
    std::size_t end = file.find("</title>");
    if(end == std::string::npos) return false;
    title = file.substr(begin + sizeof("<title>"), end - begin - sizeof("<title>"));
    return true;
}

bool ParseContent(const std::string& file, std::string& content)
{
    enum status{
        LABLE,
        CONTENT
    };
    
    enum status s = LABLE;
    for(char c : file){
        switch(s){
            case LABLE:
                if(c == '>') s = CONTENT;
                break;
            case CONTENT: 
                if(c == '<') s = LABLE;
                else{
                    if(c == '\n') c = ' ';
                    content.push_back(c);
                }
                break;
            default: 
                break;
        }
    }
    return true;
}

bool ParseUrl(const std::string& file, std::string& url)
{
    std::string url_head = "https://www.boost.org/doc/libs/1_85_0/doc/html";
    std::string url_end = file.substr(input.size());
    url = url_head + url_end;
    return true;
}

void ShowDoc(const DocInfo_t &doc)
{
    std::cout << "title: " << doc._title << std::endl;
    std::cout << "content: " << doc._content << std::endl;
    std::cout << "url: " << doc._url << std::endl;
}

bool ParseHtml(const std::vector<std::string>& file_list, std::vector<DocInfo_t> *results)
{
    //int count = 0;
    for(auto& file : file_list)
    {
        std::string result;
        if(!ns_util::FileUtil::ReadFile(file, &result)) return false;
        DocInfo_t doc;
        if(!ParseTitle(result, doc._title)){
            LOG(WARNING, file + " parse title error!");
            continue;
        } 
        if(!ParseContent(result, doc._content)){
            LOG(WARNING, file + " parse content error!");
            continue;
        } 
        if(!ParseUrl(file, doc._url)){
            LOG(WARNING, file + " parse url error!");
            continue;
        } 
        //ShowDoc(doc);
        // if((++count) % 50 == 0) {
        //     LOG(NORMAL, std::to_string(count) + " 个文件已经处理完成");
        // }
        results->push_back(std::move(doc));
    }
    return true;
}  

bool SaveHtml(const std::string& output, const std::vector<DocInfo_t>& results)
{
#define SEP '\3'
    std::ofstream out(output, std::ios::out | std::ios::binary);
    if(!out.is_open()){
        LOG(FATAL, output + " open false!");
        return false;
    }
    for(auto& item : results)
    {
        std::string s;
        s += item._title;
        s += SEP;
        s += item._content;
        s += SEP;
        s += item._url;
        s += '\n';
        out.write(s.c_str(), s.size());
    }
    out.close();
    return true;
}
